/*
 * 
 *
 * PlayerMagic.java
 *
 * Version 1.0
 *
 */

package palidino76.rs2.player.combat;

import palidino76.rs2.*;
import palidino76.rs2.util.*;
import palidino76.rs2.player.misc.*;
import palidino76.rs2.player.*;
import palidino76.rs2.player.Magic;
import palidino76.rs2.player.games.*;

public class PlayerMagic {

	Player p;
	int playerId;
	public boolean magicSpellHit;

	/**
	 * Constructor
	 */
	public PlayerMagic(Player p) {
		this.p = p;
	}

	public void createAutoCastDamageEvent() {
		Player p2 = Server.engine.players[p.enemyIndex];
		if (p == null || p2 == null || p.deathDelay > 0 || p.randomDelay > 0 || p2.deathDelay > 0 || p2.randomDelay > 0) {
			resetMagic();
			return;
		}
		if (p.autoCastDmgDelay == 0) {
			switch (p.autoCast[0]) {

				case 1: // Wind strike
			    		if (p.magicSpellHit) {
						p.magicSpellHit = false;
						p.addSkillXP(117, 6);
						p2.requestGFX(92, 100);
						p2.appendHit(getRandom(2), 0);
						p.frames.playSound(p2, 221, 1, 0);
						p.frames.playSound(p, 221, 1, 0);
						p2.requestAnim(p2.getBlockAnim1(), 0);
					} else {
						p2.requestAnim(p2.getBlockAnim1(), 0);
						p.frames.playSound(p2, 227, 1, 0);
						p.frames.playSound(p, 227, 1, 0);
						p2.requestGFX(85, 100);
					}
				break;

				case 2: // Water strike
			    		if (p.magicSpellHit) {
						p.magicSpellHit = false;
						p.addSkillXP(117, 6);
						p2.requestGFX(95, 100);
						p2.appendHit(getRandom(4), 0);
						p.frames.playSound(p2, 212, 1, 0);
						p.frames.playSound(p, 212, 1, 0);
						p2.requestAnim(p2.getBlockAnim1(), 0);
					} else {
						p2.requestAnim(p2.getBlockAnim1(), 0);
						p.frames.playSound(p2, 227, 1, 0);
						p.frames.playSound(p, 227, 1, 0);
						p2.requestGFX(85, 100);
					}
				break;

				case 3: // Earth strike
			    		if (p.magicSpellHit) {
						p.magicSpellHit = false;
						p.addSkillXP(117, 6);
						p2.requestGFX(98, 100);
						p2.appendHit(getRandom(6), 0);
						p.frames.playSound(p2, 133, 1, 0);
						p.frames.playSound(p, 133, 1, 0);
						p2.requestAnim(p2.getBlockAnim1(), 0);
					} else {
						p2.requestAnim(p2.getBlockAnim1(), 0);
						p.frames.playSound(p2, 227, 1, 0);
						p.frames.playSound(p, 227, 1, 0);
						p2.requestGFX(85, 100);
					}
				break;

				case 4: // Fire strike
			    		if (p.magicSpellHit) {
						p.magicSpellHit = false;
						p.addSkillXP(117, 6);
						p2.requestGFX(101, 100);
						p2.appendHit(getRandom(8), 0);
						p.frames.playSound(p2, 161, 1, 0);
						p.frames.playSound(p, 161, 1, 0);
						p2.requestAnim(p2.getBlockAnim1(), 0);
					} else {
						p2.requestAnim(p2.getBlockAnim1(), 0);
						p.frames.playSound(p2, 227, 1, 0);
						p.frames.playSound(p, 227, 1, 0);
						p2.requestGFX(85, 100);
					}
				break;

				case 5: // Wind bolt
			    		if (p.magicSpellHit) {
						p.magicSpellHit = false;
						p.addSkillXP(117, 6);
						p2.requestGFX(119, 100);
						p2.appendHit(getRandom(9), 0);
						p.frames.playSound(p2, 219, 1, 0);
						p.frames.playSound(p, 219, 1, 0);
						p2.requestAnim(p2.getBlockAnim1(), 0);
					} else {
						p2.requestAnim(p2.getBlockAnim1(), 0);
						p.frames.playSound(p2, 227, 1, 0);
						p.frames.playSound(p, 227, 1, 0);
						p2.requestGFX(85, 100);
					}
				break;

				case 6: // Water bolt
			    		if (p.magicSpellHit) {
						p.magicSpellHit = false;
						p.addSkillXP(117, 6);
						p2.requestGFX(122, 100);
						p2.appendHit(getRandom(10), 0);
						p.frames.playSound(p2, 210, 1, 0);
						p.frames.playSound(p, 210, 1, 0);
						p2.requestAnim(p2.getBlockAnim1(), 0);
					} else {
						p2.requestAnim(p2.getBlockAnim1(), 0);
						p.frames.playSound(p2, 227, 1, 0);
						p.frames.playSound(p, 227, 1, 0);
						p2.requestGFX(85, 100);
					}
				break;

				case 7: // Earth bolt
			    		if (p.magicSpellHit) {
						p.magicSpellHit = false;
						p.addSkillXP(117, 6);
						p2.requestGFX(125, 100);
						p2.appendHit(getRandom(11), 0);
						p.frames.playSound(p2, 131, 1, 0);
						p.frames.playSound(p, 131, 1, 0);
						p2.requestAnim(p2.getBlockAnim1(), 0);
					} else {
						p2.requestAnim(p2.getBlockAnim1(), 0);
						p.frames.playSound(p2, 227, 1, 0);
						p.frames.playSound(p, 227, 1, 0);
						p2.requestGFX(85, 100);
					}
				break;

				case 8: // Fire bolt
			    		if (p.magicSpellHit) {
						p.magicSpellHit = false;
						p.addSkillXP(117, 6);
						p2.requestGFX(128, 100);
						p2.appendHit(getRandom(12), 0);
						p.frames.playSound(p2, 158, 1, 0);
						p.frames.playSound(p, 158, 1, 0);
						p2.requestAnim(p2.getBlockAnim1(), 0);
					} else {
						p2.requestAnim(p2.getBlockAnim1(), 0);
						p.frames.playSound(p2, 227, 1, 0);
						p.frames.playSound(p, 227, 1, 0);
						p2.requestGFX(85, 100);
					}
				break;

				case 9: // Wind blast
			    		if (p.magicSpellHit) {
						p.magicSpellHit = false;
						p.addSkillXP(117, 6);
						p2.requestGFX(134, 100);
						p2.appendHit(getRandom(13), 0);
						p.frames.playSound(p2, 217, 1, 0);
						p.frames.playSound(p, 217, 1, 0);
						p2.requestAnim(p2.getBlockAnim1(), 0);
					} else {
						p2.requestAnim(p2.getBlockAnim1(), 0);
						p.frames.playSound(p2, 227, 1, 0);
						p.frames.playSound(p, 227, 1, 0);
						p2.requestGFX(85, 100);
					}
				break;

				case 10: // Water blast
			    		if (p.magicSpellHit) {
						p.magicSpellHit = false;
						p.addSkillXP(117, 6);
						p2.requestGFX(137, 100);
						p2.appendHit(getRandom(14), 0);
						p.frames.playSound(p2, 208, 1, 0);
						p.frames.playSound(p, 208, 1, 0);
						p2.requestAnim(p2.getBlockAnim1(), 0);
					} else {
						p2.requestAnim(p2.getBlockAnim1(), 0);
						p.frames.playSound(p2, 227, 1, 0);
						p.frames.playSound(p, 227, 1, 0);
						p2.requestGFX(85, 100);
					}
				break;

				case 11: // Earth blast
			    		if (p.magicSpellHit) {
						p.magicSpellHit = false;
						p.addSkillXP(117, 6);
						p2.requestGFX(140, 100);
						p2.appendHit(getRandom(15), 0);
						p.frames.playSound(p2, 129, 1, 0);
						p.frames.playSound(p, 129, 1, 0);
						p2.requestAnim(p2.getBlockAnim1(), 0);
					} else {
						p2.requestAnim(p2.getBlockAnim1(), 0);
						p.frames.playSound(p2, 227, 1, 0);
						p.frames.playSound(p, 227, 1, 0);
						p2.requestGFX(85, 100);
					}
				break;

				case 12: // Fire blast
			    		if (p.magicSpellHit) {
						p.magicSpellHit = false;
						p.addSkillXP(117, 6);
						p2.requestGFX(131, 100);
						p2.appendHit(getRandom(16), 0);
						p.frames.playSound(p2, 156, 1, 0);
						p.frames.playSound(p, 156, 1, 0);
						p2.requestAnim(p2.getBlockAnim1(), 0);
					} else {
						p2.requestAnim(p2.getBlockAnim1(), 0);
						p.frames.playSound(p2, 227, 1, 0);
						p.frames.playSound(p, 227, 1, 0);
						p2.requestGFX(85, 100);
					}
				break;

				case 13:
			    		if (p.magicSpellHit) {
						p.magicSpellHit = false;
						p.addSkillXP(117, 6);
						p2.requestGFX(160, 100);
						p2.appendHit(getRandom(17), 0);
						p.frames.playSound(p2, 223, 1, 0);
						p.frames.playSound(p, 223, 1, 0);
						p2.requestAnim(p2.getBlockAnim1(), 0);
					} else {
						p2.requestAnim(p2.getBlockAnim1(), 0);
						p.frames.playSound(p2, 227, 1, 0);
						p.frames.playSound(p, 227, 1, 0);
						p2.requestGFX(85, 100);
					}
				break;

				case 14:
			    		if (p.magicSpellHit) {
						p.magicSpellHit = false;
						p.addSkillXP(117, 6);
						p2.requestGFX(163, 100);
						p2.appendHit(getRandom(18), 0);
						p.frames.playSound(p2, 214, 1, 0);
						p.frames.playSound(p, 214, 1, 0);
						p2.requestAnim(p2.getBlockAnim1(), 0);
					} else {
						p2.requestAnim(p2.getBlockAnim1(), 0);
						p.frames.playSound(p2, 227, 1, 0);
						p.frames.playSound(p, 227, 1, 0);
						p2.requestGFX(85, 100);
					}
				break;

				case 15:
			    		if (p.magicSpellHit) {
						p.magicSpellHit = false;
						p.addSkillXP(117, 6);
						p2.requestGFX(166, 100);
						p2.appendHit(getRandom(19), 0);
						p.frames.playSound(p2, 163, 1, 0);
						p.frames.playSound(p, 163, 1, 0);
						p2.requestAnim(p2.getBlockAnim1(), 0);
					} else {
						p2.requestAnim(p2.getBlockAnim1(), 0);
						p.frames.playSound(p2, 227, 1, 0);
						p.frames.playSound(p, 227, 1, 0);
						p2.requestGFX(85, 100);
					}
				break;

				case 16:
			    		if (p.magicSpellHit) {
						p.magicSpellHit = false;
						p.addSkillXP(117, 6);
						p2.requestGFX(157, 100);
						p2.appendHit(getRandom(20), 0);
						p.frames.playSound(p2, 163, 1, 0);
						p.frames.playSound(p, 163, 1, 0);
						p2.requestAnim(p2.getBlockAnim1(), 0);
					} else {
						p2.requestAnim(p2.getBlockAnim1(), 0);
						p.frames.playSound(p2, 227, 1, 0);
						p.frames.playSound(p, 227, 1, 0);
						p2.requestGFX(85, 100);
					}
				break;

				case 18: // Bind
			    		if (p.magicSpellHit) {
						p.magicSpellHit = false;
						p.addSkillXP(100, 6);
						p2.requestGFX(181, 100);
						p2.freezeDelay = 6;
						Server.engine.playerMovement.resetWalkingQueue(p2);
						p2.appendHit(getRandom(2), 0);
						p.frames.playSound(p2, 99, 1, 0);
						p.frames.playSound(p, 99, 1, 0);
						p2.requestAnim(p2.getBlockAnim1(), 0);
					} else {
						p2.requestAnim(p2.getBlockAnim1(), 0);
						p.frames.playSound(p2, 227, 1, 0);
						p.frames.playSound(p, 227, 1, 0);
						p2.requestGFX(85, 100);
					}
				break;

				case 19: // Snare
			    		if (p.magicSpellHit) {
						p.magicSpellHit = false;
						p.addSkillXP(100, 6);
						p2.requestGFX(180, 100);
						p2.freezeDelay = 6;
						Server.engine.playerMovement.resetWalkingQueue(p2);
						p2.appendHit(getRandom(2), 0);
						p.frames.playSound(p2, 3002, 1, 0);
						p.frames.playSound(p, 3002, 1, 0);
						p2.requestAnim(p2.getBlockAnim1(), 0);
					} else {
						p2.requestAnim(p2.getBlockAnim1(), 0);
						p.frames.playSound(p2, 227, 1, 0);
						p.frames.playSound(p, 227, 1, 0);
						p2.requestGFX(85, 100);
					}
				break;

				case 20: // Entangle
			    		if (p.magicSpellHit) {
						p.magicSpellHit = false;
						p.addSkillXP(100, 6);
						p2.requestGFX(179, 100);
						p2.freezeDelay = 20;
						Server.engine.playerMovement.resetWalkingQueue(p2);
						p2.appendHit(getRandom(5), 0);
						p.frames.playSound(p2, 153, 1, 0);
						p.frames.playSound(p, 153, 1, 0);
						p2.requestAnim(p2.getBlockAnim1(), 0);
					} else {
						p2.requestAnim(p2.getBlockAnim1(), 0);
						p.frames.playSound(p2, 227, 1, 0);
						p.frames.playSound(p, 227, 1, 0);
						p2.requestGFX(85, 100);
					}
				break;

				case 21: // Saradomin Strike
			    		if (p.magicSpellHit) {
						p.magicSpellHit = false;
						p.addSkillXP(117, 6);
						p2.requestGFX(76, 100);
						p2.appendHit(getRandom(30), 0);
						p2.requestAnim(p2.getBlockAnim1(), 0);
					} else {
						p2.requestAnim(p2.getBlockAnim1(), 0);
						p.frames.playSound(p2, 227, 1, 0);
						p.frames.playSound(p, 227, 1, 0);
						p2.requestGFX(85, 100);
					}
				break;

				case 22: // Claws of Guthix
			    		if (p.magicSpellHit) {
						p.magicSpellHit = false;
						p.addSkillXP(117, 6);
						p2.requestGFX(77, 100);
						p2.appendHit(getRandom(30), 0);
						p2.requestAnim(p2.getBlockAnim1(), 0);
					} else {
						p2.requestAnim(p2.getBlockAnim1(), 0);
						p.frames.playSound(p2, 227, 1, 0);
						p.frames.playSound(p, 227, 1, 0);
						p2.requestGFX(85, 100);
					}
				break;

				case 23: // Flames of Zamorak
			    		if (p.magicSpellHit) {
						p.magicSpellHit = false;
						p.addSkillXP(117, 6);
						p2.requestGFX(78, 0);
						p2.appendHit(getRandom(30), 0);
						p2.requestAnim(p2.getBlockAnim1(), 0);
					} else {
						p2.requestAnim(p2.getBlockAnim1(), 0);
						p.frames.playSound(p2, 227, 1, 0);
						p.frames.playSound(p, 227, 1, 0);
						p2.requestGFX(85, 100);
					}
				break;

			}
			switch (p.autoCast[1]) {

				case 8:
			    		if (p.magicSpellHit) {
						p.magicSpellHit = false;
						p.addSkillXP(117, 6);
						p2.appendHit(getRandom(22), 0);
						p2.requestGFX(363, 0);
						p.frames.playSound(p2, 179, 1, 0);
						p.frames.playSound(p, 170, 1, 0);
						p2.requestAnim(p2.getBlockAnim1(), 0);
					} else {
						p2.requestAnim(p2.getBlockAnim1(), 0);
						p.frames.playSound(p2, 227, 1, 0);
						p.frames.playSound(p, 227, 1, 0);
						p2.requestGFX(85, 100);
					}
				break;

				case 11:
			    		if (p.magicSpellHit) {
						p.magicSpellHit = false;
						p.addSkillXP(117, 6);
						p2.appendHit(getRandom(24), 0);
						p2.requestGFX(375, 0);
						p.frames.playSound(p2, 107, 1, 0);
						p.frames.playSound(p, 107, 1, 0);
						p2.requestAnim(p2.getBlockAnim1(), 0);
					} else {
						p2.requestAnim(p2.getBlockAnim1(), 0);
						p.frames.playSound(p2, 227, 1, 0);
						p.frames.playSound(p, 227, 1, 0);
						p2.requestGFX(85, 100);
					}
				break;

				case 12:
			    		if (p.magicSpellHit) {
						p.magicSpellHit = false;
						p.addSkillXP(117, 6);
						p2.appendHit(getRandom(25), 0);
						p2.requestGFX(367, 0);
						p.frames.playSound(p2, 169, 1, 0);
						p.frames.playSound(p, 169, 1, 0);
						p2.requestAnim(p2.getBlockAnim1(), 0);
					} else {
						p2.requestAnim(p2.getBlockAnim1(), 0);
						p.frames.playSound(p2, 227, 1, 0);
						p.frames.playSound(p, 227, 1, 0);
						p2.requestGFX(85, 100);
					}
				break;

				case 13: // Smoke barrage
			    		if (p.magicSpellHit) {
						p.magicSpellHit = false;
						p.addSkillXP(200, 6);
						p2.requestGFX(391, 0);
						p2.appendHit(getRandom(28), 0);
						p.frames.playSound(p2, 182, 1, 0);
						p.frames.playSound(p, 182, 1, 0);
						p2.requestAnim(p2.getBlockAnim1(), 0);
					} else {
						p2.requestAnim(p2.getBlockAnim1(), 0);
						p.frames.playSound(p2, 227, 1, 0);
						p.frames.playSound(p, 227, 1, 0);
						p2.requestGFX(85, 100);
					}
				break;

				case 14: // Shadow Barrage
			    		if (p.magicSpellHit) {
						p.magicSpellHit = false;
						p.addSkillXP(200, 6);
						p2.requestGFX(383, 0);
						p2.appendHit(getRandom(28), 0);
						p2.requestAnim(p2.getBlockAnim1(), 0);
					} else {
						p2.requestAnim(p2.getBlockAnim1(), 0);
						p.frames.playSound(p2, 227, 1, 0);
						p.frames.playSound(p, 227, 1, 0);
						p2.requestGFX(85, 100);
					}
				break;

				case 15: // Blood barrage 
			    		if (p.magicSpellHit) {
						p.magicSpellHit = false;
						p2.requestGFX(377, 0);
						p2.appendHit(getRandom(29), 0);
						p2.requestAnim(p2.getBlockAnim1(), 0);
					} else {
						p2.requestAnim(p2.getBlockAnim1(), 0);
						p.frames.playSound(p2, 227, 1, 0);
						p.frames.playSound(p, 227, 1, 0);
						p2.requestGFX(85, 100);
					}
				break;

				case 16:
			    		if (p.magicSpellHit) {
						p.magicSpellHit = false;
						p.addSkillXP(200, 6);
						p.frames.playSound(p2, 168, 1, 0);
						p.frames.playSound(p, 168, 1, 0);
						p2.requestGFX(369, 0);
						p2.appendHit(getRandom(30), 0);
						p2.requestAnim(p2.getBlockAnim1(), 0);
					} else {
						p2.requestAnim(p2.getBlockAnim1(), 0);
						p.frames.playSound(p2, 227, 1, 0);
						p.frames.playSound(p, 227, 1, 0);
						p2.requestGFX(85, 100);
					}
				break;
			}
			p.autoCastDmgDelay = -1;
		}
	}

	/**
	 * Creates an auto cast system.
	 */
	public void createAutoCastEvent() {
		Player p2 = Server.engine.players[p.enemyIndex];
		if (p == null || p2 == null || p.deathDelay > 0 || p.randomDelay > 0 || p2.deathDelay > 0 || p2.randomDelay > 0) {
			resetMagic();
			return;
		}
		int casterX = p.absX;
		int casterY = p.absY;
		int offsetY = (p.absY - p2.absY) * -1;
		int offsetX = (p.absX - p2.absX) * -1;
		int interfaceId = p.stream.readUnsignedWord();
		Server.engine.playerMovement.resetWalkingQueue(p);
		PlayerCombat PC = new PlayerCombat(p);
		if (p.inEdgePk() && p2.inEdgePk()) {
			if (p2.enemyIndex != p.playerId && p2.enemyIndex != 0) {
				resetMagic();
				p.frames.sendMessage(p, "This player is already in combat.");
				return;
			}
		}
		if (!Server.engine.wildernessArea(p2.absX, p2.absY) && !p2.duelFight() && !p2.AtPits()) {
			p.frames.sendMessage(p, "You cannot attack other players in this area.");
			resetMagic();
			return;
		}
		if (!Server.engine.wildernessArea(p.absX, p.absY) && !p.duelFight() && !p.AtPits()) {
			p.frames.sendMessage(p, "You cannot attack other players in this area.");
			resetMagic();
			return;
		}
		if (!PC.isInWildRange(p) && !p.duelFight()) {
			if (interfaceId != 430) {
				p.requestFaceTo(p.enemyIndex + 32768);
				p.requestFaceTo(65535);
				stopMove(p);
				p.frames.sendMessage(p, "Your level difference is too great!");
				p.frames.sendMessage(p, "You need to move further into the Wilderness.");
				resetMagic();
				return;
			}
		}
		if (p.equipment[1] == 4041 && p2.equipment[1] == 4041) {
	   		p.frames.sendMessage(p, "You cannot attack a player that is on your team.");
			resetMagic();
	   		return;
		    }
		if (p.equipment[1] == 4042 && p2.equipment[1] == 4042) {
			p.frames.sendMessage(p, "You cannot attack a player that is on your team.");
			resetMagic();
			return;
		}

		if (p.inEdgePk() && p2.inEdgePk() || !p.multiZone() && !p2.multiZone()) {
			if (p.combatWith != p2.playerId && p.combatWith != 0) {
				p.requestFaceTo(p.enemyIndex + 32768);
				p.requestFaceTo(65535);
				p.resetAttack();
				p.frames.sendMessage(p, "You are already in combat.");
				return;
			}
			if (p2.combatWith != p.playerId && p2.combatWith != 0) {
				p.requestFaceTo(p.enemyIndex + 32768);
				p.requestFaceTo(65535);
				p.resetAttack();
				p.frames.sendMessage(p, "That player is already in combat.");
				return;
			}
		}

		if (Misc.getDistance(p.absX, p.absY, p2.absX, p.absY) > 8)
			return;
		p.requestFaceTo(p2.playerId + 32768);
		switch (p.autoCast[0]) {

			case 16: //Fire wave Autocasting
                        if (hasReq(p, blood, 1) && hasReq(p, air, 5) && hasReq(p, fire, 7)) {
			    if (getRandom(magicAttack()) >= getRandom(magicDefence())) {
				p.magicSpellHit = true;
			    } else {
				p.magicSpellHit = false;
			    }
				p.autoCast[1] = 0;
				p.combatDelay = 6;
                                stopMove(p);
                            	deleteItems(p, blood, 1, air, 5, fire, 7);
				p.frames.playSound(p2, 162, 1, 0);
				p.frames.playSound(p, 162, 1, 0);
				autoCastEngine(casterX, casterY, offsetX, offsetY, 1167, 155, 100, 156, 5, p2.playerId, 3);
                        } else {
                            	p.frames.sendMessage(p, "You dont have the required runes for this spell.");
                        }
			break;

			case 15: //Earth wave Autocasting
                        if (hasReq(p, blood, 1) && hasReq(p, air, 5) && hasReq(p, earth, 7)) {
			    if (getRandom(magicAttack()) >= getRandom(magicDefence())) {
				p.magicSpellHit = true;
			    } else {
				p.magicSpellHit = false;
			    }
				p.autoCast[1] = 0;
				p.combatDelay = 6;
                                stopMove(p);
                            	deleteItems(p, blood, 1, air, 5, earth, 7);
				p.frames.playSound(p2, 134, 1, 0);
				p.frames.playSound(p, 134, 1, 0);
				autoCastEngine(casterX, casterY, offsetX, offsetY, 1167, 164, 100, 165, 5, p2.playerId, 3);
                        } else {
                            	p.frames.sendMessage(p, "You dont have the required runes for this spell.");
                        }
			break;

			case 14: //Water wave Autocasting
                        if (hasReq(p, blood, 1) && hasReq(p, air, 5) && hasReq(p, water, 7)) {
			    if (getRandom(magicAttack()) >= getRandom(magicDefence())) {
				p.magicSpellHit = true;
			    } else {
				p.magicSpellHit = false;
			    }
				p.autoCast[1] = 0;
				p.combatDelay = 6;
                                stopMove(p);
                            	deleteItems(p, blood, 1, air, 5, fire, 7);
				p.frames.playSound(p2, 213, 1, 0);
				p.frames.playSound(p, 213, 1, 0);
				autoCastEngine(casterX, casterY, offsetX, offsetY, 1167, 161, 100, 162, 5, p2.playerId, 3);
                        } else {
                            	p.frames.sendMessage(p, "You dont have the required runes for this spell.");
                        }
			break;

			case 13: //Wind wave Autocasting
                        if (hasReq(p, blood, 1) && hasReq(p, air, 5)) {
			    if (getRandom(magicAttack()) >= getRandom(magicDefence())) {
				p.magicSpellHit = true;
			    } else {
				p.magicSpellHit = false;
			    }
				p.autoCast[1] = 0;
				p.combatDelay = 6;
                                stopMove(p);
                            	deleteItems(p, blood, 1, air, 5);
				p.frames.playSound(p2, 222, 1, 0);
				p.frames.playSound(p, 222, 1, 0);
				autoCastEngine(casterX, casterY, offsetX, offsetY, 1167, 158, 100, 159, 5, p2.playerId, 3);
                        } else {
                            	p.frames.sendMessage(p, "You dont have the required runes for this spell.");
                        }
			break;

			case 12: //Fire blast Autocasting
                        if (hasReq(p, death, 1) && hasReq(p, air, 4) && hasReq(p, fire, 5)) {
			    if (getRandom(magicAttack()) >= getRandom(magicDefence())) {
				p.magicSpellHit = true;
			    } else {
				p.magicSpellHit = false;
			    }
				p.autoCast[1] = 0;
				p.combatDelay = 6;
                                stopMove(p);
                            	deleteItems(p, death, 1, air, 4, fire, 5);
				p.frames.playSound(p2, 155, 1, 0);
				p.frames.playSound(p, 155, 1, 0);
				autoCastEngine(casterX, casterY, offsetX, offsetY, 1162, 129, 100, 130, 5, p2.playerId, 3);
                        } else {
                            	p.frames.sendMessage(p, "You dont have the required runes for this spell.");
                        }
			break;

			case 11: //Earth blast Autocasting
                        if (hasReq(p, death, 1) && hasReq(p, air, 3) && hasReq(p, earth, 4)) {
			    if (getRandom(magicAttack()) >= getRandom(magicDefence())) {
				p.magicSpellHit = true;
			    } else {
				p.magicSpellHit = false;
			    }
				p.autoCast[1] = 0;
				p.combatDelay = 6;
                                stopMove(p);
                            	deleteItems(p, death, 1, air, 3, earth, 4);
				p.frames.playSound(p2, 128, 1, 0);
				p.frames.playSound(p, 128, 1, 0);
				autoCastEngine(casterX, casterY, offsetX, offsetY, 1162, 138, 100, 139, 5, p2.playerId, 3);
                        } else {
                            	p.frames.sendMessage(p, "You dont have the required runes for this spell.");
                        }
			break;

			case 10: //Water blast Autocasting
                        if (hasReq(p, death, 1) && hasReq(p, air, 3) && hasReq(p, water, 3)) {
			    if (getRandom(magicAttack()) >= getRandom(magicDefence())) {
				p.magicSpellHit = true;
			    } else {
				p.magicSpellHit = false;
			    }
				p.autoCast[1] = 0;
				p.combatDelay = 6;
                                stopMove(p);
                            	deleteItems(p, death, 1, air, 3, water, 3);
				p.frames.playSound(p2, 207, 1, 0);
				p.frames.playSound(p, 207, 1, 0);
				autoCastEngine(casterX, casterY, offsetX, offsetY, 1162, 135, 100, 136, 5, p2.playerId, 3);
                        } else {
                            	p.frames.sendMessage(p, "You dont have the required runes for this spell.");
                        }
			break;

			case 9: //Wind blast Autocasting
                        if (hasReq(p, death, 1) && hasReq(p, air, 3)) {
			    if (getRandom(magicAttack()) >= getRandom(magicDefence())) {
				p.magicSpellHit = true;
			    } else {
				p.magicSpellHit = false;
			    }
				p.autoCast[1] = 0;
				p.combatDelay = 6;
                                stopMove(p);
                            	deleteItems(p, death, 1, air, 3);
				p.frames.playSound(p2, 216, 1, 0);
				p.frames.playSound(p, 216, 1, 0);
				autoCastEngine(casterX, casterY, offsetX, offsetY, 1162, 132, 100, 133, 5, p2.playerId, 3);
                        } else {
                            	p.frames.sendMessage(p, "You dont have the required runes for this spell.");
                        }
			break;

			case 8: //Fire bolt Autocasting
                        if (hasReq(p, chaos, 1) && hasReq(p, air, 2) && hasReq(p, fire, 3)) {
			    if (getRandom(magicAttack()) >= getRandom(magicDefence())) {
				p.magicSpellHit = true;
			    } else {
				p.magicSpellHit = false;
			    }
				p.autoCast[1] = 0;
				p.combatDelay = 6;
                                stopMove(p);
                            	deleteItems(p, chaos, 1, air, 2, fire, 3);
				p.frames.playSound(p2, 157, 1, 0);
				p.frames.playSound(p, 157, 1, 0);
				autoCastEngine(casterX, casterY, offsetX, offsetY, 1162, 126, 100, 127, 5, p2.playerId, 3);
                        } else {
                            	p.frames.sendMessage(p, "You dont have the required runes for this spell.");
                        }
			break;

			case 7: //Earth bolt Autocasting
                        if (hasReq(p, chaos, 1) && hasReq(p, air, 2) && hasReq(p, earth, 3)) {
			    if (getRandom(magicAttack()) >= getRandom(magicDefence())) {
				p.magicSpellHit = true;
			    } else {
				p.magicSpellHit = false;
			    }
				p.autoCast[1] = 0;
				p.combatDelay = 6;
                                stopMove(p);
                            	deleteItems(p, chaos, 1, air, 2, earth, 3);
				p.frames.playSound(p2, 130, 1, 0);
				p.frames.playSound(p, 130, 1, 0);
				autoCastEngine(casterX, casterY, offsetX, offsetY, 1162, 123, 100, 124, 5, p2.playerId, 3);
                        } else {
                            	p.frames.sendMessage(p, "You dont have the required runes for this spell.");
                        }
			break;

			case 6: //Water bolt Autocasting
                        if (hasReq(p, chaos, 1) && hasReq(p, air, 2) && hasReq(p, water, 2)) {
			    if (getRandom(magicAttack()) >= getRandom(magicDefence())) {
				p.magicSpellHit = true;
			    } else {
				p.magicSpellHit = false;
			    }
				p.autoCast[1] = 0;
				p.combatDelay = 6;
                                stopMove(p);
                            	deleteItems(p, chaos, 1, air, 2, water, 2);
				p.frames.playSound(p2, 209, 1, 0);
				p.frames.playSound(p, 209, 1, 0);
				autoCastEngine(casterX, casterY, offsetX, offsetY, 1162, 120, 100, 121, 5, p2.playerId, 3);
                        } else {
                            	p.frames.sendMessage(p, "You dont have the required runes for this spell.");
                        }
			break;

			case 5: //Wind bolt Autocasting
                        if (hasReq(p, chaos, 1) && hasReq(p, air, 2)) {
			    if (getRandom(magicAttack()) >= getRandom(magicDefence())) {
				p.magicSpellHit = true;
			    } else {
				p.magicSpellHit = false;
			    }
				p.autoCast[1] = 0;
				p.combatDelay = 6;
                                stopMove(p);
                            	deleteItems(p, chaos, 1, air, 2);
				p.frames.playSound(p2, 218, 1, 0);
				p.frames.playSound(p, 218, 1, 0);
				autoCastEngine(casterX, casterY, offsetX, offsetY, 1162, 117, 100, 118, 5, p2.playerId, 3);
                        } else {
                            	p.frames.sendMessage(p, "You dont have the required runes for this spell.");
                        }
			break;

			case 4: //Fire strike Autocasting
                        if (hasReq(p, mind, 1) && hasReq(p, air, 2) && hasReq(p, fire, 3)) {
			    if (getRandom(magicAttack()) >= getRandom(magicDefence())) {
				p.magicSpellHit = true;
			    } else {
				p.magicSpellHit = false;
			    }
				p.autoCast[1] = 0;
				p.combatDelay = 6;
                                stopMove(p);
                            	deleteItems(p, air, 2, mind, 1, fire, 3);
				p.frames.playSound(p2, 160, 1, 0);
				p.frames.playSound(p, 160, 1, 0);
				autoCastEngine(casterX, casterY, offsetX, offsetY, 1162, 99, 100, 100, 5, p2.playerId, 3);
                        } else {
                            	p.frames.sendMessage(p, "You dont have the required runes for this spell.");
                        }
			break;

			case 3: //Earth strike Autocasting
                        if (hasReq(p, mind, 1) && hasReq(p, air, 2) && hasReq(p, earth, 2)) {
			    if (getRandom(magicAttack()) >= getRandom(magicDefence())) {
				p.magicSpellHit = true;
			    } else {
				p.magicSpellHit = false;
			    }
				p.autoCast[1] = 0;
				p.combatDelay = 6;
                                stopMove(p);
                            	deleteItems(p, air, 1, mind, 1, earth, 2);
				p.frames.playSound(p2, 132, 1, 0);
				p.frames.playSound(p, 132, 1, 0);
				autoCastEngine(casterX, casterY, offsetX, offsetY, 1162, 96, 100, 97, 5, p2.playerId, 3);
                        } else {
                            	p.frames.sendMessage(p, "You dont have the required runes for this spell.");
                        }
			break;

			case 2: //Water strike Autocasting
                        if (hasReq(p, mind, 1) && hasReq(p, air, 1) && hasReq(p, water, 1)) {
			    if (getRandom(magicAttack()) >= getRandom(magicDefence())) {
				p.magicSpellHit = true;
			    } else {
				p.magicSpellHit = false;
			    }
				p.autoCast[1] = 0;
				p.combatDelay = 6;
                                stopMove(p);
                            	deleteItems(p, air, 1, mind, 1, water, 1);
				p.frames.playSound(p2, 211, 1, 0);
				p.frames.playSound(p, 211, 1, 0);
				autoCastEngine(casterX, casterY, offsetX, offsetY, 1162, 93, 100, 94, 5, p2.playerId, 3);
                        } else {
                            	p.frames.sendMessage(p, "You dont have the required runes for this spell.");
                        }
			break;

			case 1: //Wind strike Autocasting
                        if (hasReq(p, mind, 1) && hasReq(p, air, 1)) {
			    if (getRandom(magicAttack()) >= getRandom(magicDefence())) {
				p.magicSpellHit = true;
			    } else {
				p.magicSpellHit = false;
			    }
				p.autoCast[1] = 0;
				p.combatDelay = 6;
                                stopMove(p);
                            	deleteItems(p, air, 1, mind, 1);
				p.frames.playSound(p2, 220, 1, 0);
				p.frames.playSound(p, 220, 1, 0);
				autoCastEngine(casterX, casterY, offsetX, offsetY, 1162, 90, 100, 91, 5, p2.playerId, 3);
                        } else {
                            	p.frames.sendMessage(p, "You dont have the required runes for this spell.");
                        }
			break;

		}
		switch (p.autoCast[1]) {

			case 8: //Ice blitz Autocasting
			    if (getRandom(magicAttack()) >= getRandom(magicDefence())) {
				Server.engine.playerMovement.resetWalkingQueue(p2);
				p.magicSpellHit = true;
				p2.freezeDelay = 12;
			    } else {
				p.magicSpellHit = false;
			    }
				p.autoCastDmgDelay = 4;
				p.autoCastDelay = 5;
				p.frames.playSound(p, 171, 1, 0);
				p.frames.playSound(p2, 171, 1, 0);
				p.requestAnim(1979, 0);
				p.autoCast[0] = 0;
                                stopMove(p);
				deleteItems(p, death, 2, water, 4, chaos, 4);
			break;

			case 12: //Ice blitz Autocasting
			    if (getRandom(magicAttack()) >= getRandom(magicDefence())) {
				Server.engine.playerMovement.resetWalkingQueue(p2);
				p.magicSpellHit = true;
				p2.freezeDelay = 15;
			    } else {
				p.magicSpellHit = false;
			    }
				p.autoCastDmgDelay = 4;
				p.autoCastDelay = 5;
				p.requestGFX(366, 0);
				p.frames.playSound(p, 171, 1, 0);
				p.frames.playSound(p2, 171, 1, 0);
				p.requestAnim(1978, 0);
				p.autoCast[0] = 0;
                                stopMove(p);
				deleteItems(p, death, 2, water, 3, blood, 2);
			break;

			case 16: //Ice barrage Autocasting
			    if (getRandom(magicAttack()) >= getRandom(magicDefence())) {
				Server.engine.playerMovement.resetWalkingQueue(p2);
				p.magicSpellHit = true;
				p2.freezeDelay = 20;
			    } else {
				p.magicSpellHit = false;
			    }
				p.autoCastDmgDelay = 4;
				p.autoCastDelay = 5;
				p.frames.playSound(p2, 171, 1, 0);
				p.frames.playSound(p, 171, 1, 0);
				p.requestAnim(1979, 0);
				p.autoCast[0] = 0;
                                stopMove(p);
				deleteItems(p, death, 4, water, 6, blood, 2);
				//autoCastEngine(casterX, casterY, offsetX, offsetY, 1979, -1, 0, 368, 6, p2.playerId, 3);
			break;
		}
	}

	/**
	 * Autocasting variable setts
	 * @param animRequested the anim thats going to be displayed.
	 * @param graphicRequested the graphic thats going to be displayed.
	 * @param projectileRequested the requested projectile to make the autocasting look realistic.
	 * @param delayRequested the delay between fighting the enemy (Anti massing)
	 */
	public void autoCastEngine(int casterX, int casterY, int offsetX, int offsetY, int animRequested, int graphicRequested, int graphicHeight,
			int projectileRequested, int delayRequested, int enemyIndex, int damageDelay) {
		p.autoCastDmgDelay = damageDelay;
		p.requestGFX(graphicRequested, graphicHeight);
		p.requestAnim(animRequested, 0);
		p.autoCastDelay = delayRequested;
		p.frames.addProjectile(p, casterY, casterX, offsetY, offsetX, 50, 100, projectileRequested, 46, 31, enemyIndex, true);
	}

	int magicAttack() {
		if (p == null) {
			return 0;
		}
		return (p.skillLvl[6] + p.equipmentBonus[3] + p.equipmentBonus[3]);
	}

	int magicDefence() {
			Player p2 = Server.engine.players[p.enemyIndex];
		if (p == null) {
			return 0;
		}
	    	if (!p.magicPrayer) {
			return (int)((p.skillLvl[1] / 3) + (p2.skillLvl[6] / 2) + p.equipmentBonus[8]);
	    	} else if (p.magicPrayer) {
			return (int)((p.skillLvl[1] / 2) + (p2.skillLvl[6] / 4) + p.equipmentBonus[8] + 50);
	    	} else {
			return 0;
		}
	}

    public static void stopMove(Player p) {
        Server.engine.playerMovement.resetWalkingQueue(p);
    }


    public static void deleteItems(Player p, int item1id, int item1amt, int item2id, int item2amt) {
        Engine.playerItems.deleteItem(p, item1id,
                Engine.playerItems.getItemSlot(p, item1id), item1amt);
        Engine.playerItems.deleteItem(p, item2id,
                Engine.playerItems.getItemSlot(p, item2id), item2amt);
    }

    public static void deleteItems(Player p, int item1id, int item1amt, int item2id, int item2amt, int item3id, int item3amt) {
        Engine.playerItems.deleteItem(p, item1id,
                Engine.playerItems.getItemSlot(p, item1id), item1amt);
        Engine.playerItems.deleteItem(p, item2id,
                Engine.playerItems.getItemSlot(p, item2id), item2amt);
        Engine.playerItems.deleteItem(p, item3id,
                Engine.playerItems.getItemSlot(p, item3id), item3amt);
    }
    
    public static void deleteItems(Player p, int item1id, int item1amt, int item2id, int item2amt, int item3id, int item3amt, int item4id, int item4amt) {
        Engine.playerItems.deleteItem(p, item1id,
                Engine.playerItems.getItemSlot(p, item1id), item1amt);
        Engine.playerItems.deleteItem(p, item2id,
                Engine.playerItems.getItemSlot(p, item2id), item2amt);
        Engine.playerItems.deleteItem(p, item3id,
                Engine.playerItems.getItemSlot(p, item3id), item3amt);
        Engine.playerItems.deleteItem(p, item4id,
                Engine.playerItems.getItemSlot(p, item4id), item4amt);
    }

	/**
	 * Emote checking.
	 */
	int getMagicAnim() {
		switch (p.spellType) {

			case 3:
				return 1979;

			case 41:
				return 811;

			case 56:
				return 710;

			case 48:
			case 55:
				return 1167;

			default:
				return -1;

		}
	}

	/**
	 * Resets attacking with magic.
	 */
	void resetMagic() {
		PlayerMethods pm = new PlayerMethods(p);
		playerId = 0;
		pm.setCombatDelay(0);
		p.autoCast[0] = p.autoCast[1] = 0;
		p.castAuto = false;
		p.usingAutoCast = false;
		p.autoCastDelay = 0;
	}

    public static boolean hasReq(Player p, int ItemId, int amt) {
        return Engine.playerItems.haveItem(p, ItemId, amt);
    }

    public static int fire = 554;
    public static int water = 555;
    public static int air = 556;
    public static int earth = 557;
    public static int mind = 558;
    public static int body = 559;
    public static int law = 563;
    public static int cosmic = 564;
    public static int death = 560;
    public static int nature = 561;
    public static int chaos = 562;
    public static int blood = 565;
    public static int soul = 566;
    public static int bones = 526;

	/**
	 * Calculates a random number within a range.
	 */
    	private int getRandom(int range) {
        	return (int)(Math.random() * (range + 1));
    	}

}